import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateTeacherDto } from './dto/create-teacher.dto';
import { UpdateTeacherDto } from './dto/update-teacher.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { TeacherEntity } from './entities/teacher.entity';
import { Repository } from 'typeorm';

@Injectable()
export class TeacherService {
  constructor(
    @InjectRepository(TeacherEntity)
    private readonly teacherRepository: Repository<TeacherEntity>,
  ) {}
  async create(createTeacherDto: CreateTeacherDto) {
    // return await this.teacherRepository.save(createTeacherDto);
    const query = `insert into teacher (name, phone) value ("${createTeacherDto.name}", "${createTeacherDto.phone}")`;
    return await this.teacherRepository.query(query);
  }

  async findAll() {
    // return await this.teacherRepository.find();
    return await this.teacherRepository.query('select * from teacher');
  }

  async findOne(id: number): Promise<UpdateTeacherDto> {
    // return await this.teacherRepository.findBy({ id });
    const teacherEntity = await this.teacherRepository.query(
      `select * from teacher where id = ${id}`,
    );
    if (!teacherEntity) {
      throw new NotFoundException('Teacher not found');
    }
    return teacherEntity;
  }

  update(updateTeacherDto: UpdateTeacherDto) {
    // return `This action updates a #${id} teacher`;
    const query = `update teacher set name="${updateTeacherDto.name}", phone="${updateTeacherDto.phone}" where id = ${updateTeacherDto.id}`;
    return this.teacherRepository.query(query);
  }

  remove(id: number) {
    // return `This action removes a #${id} teacher`;
    const query = `delete from teacher where id = ${id}`;
    return this.teacherRepository.query(query);
  }
}
